package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.MovieDetailActivity;
import flc.ast.activity.PraiseTeleplayActivity;
import flc.ast.activity.ScoreMovieActivity;
import flc.ast.activity.TeleplayListActivity;
import flc.ast.activity.TicketMovieActivity;
import flc.ast.adapter.HomeMovieAdapter;
import flc.ast.adapter.TeleplayAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private HomeMovieAdapter movieAdapter;
    private TeleplayAdapter teleplayAdapter;
    private List<StkResBean> listMovie = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivScoreMovie.setOnClickListener(this);
        mDataBinding.ivTeleplayList.setOnClickListener(this);
        mDataBinding.ivTicketMovieMore.setOnClickListener(this);
        mDataBinding.ivPraiseTeleplayMore.setOnClickListener(this);

        mDataBinding.rvTicketMovieList.setLayoutManager(new GridLayoutManager(mContext, 2));
        movieAdapter = new HomeMovieAdapter();
        mDataBinding.rvTicketMovieList.setAdapter(movieAdapter);
        movieAdapter.setOnItemClickListener(this);

        mDataBinding.rvPraiseTeleplayList.setLayoutManager(new GridLayoutManager(mContext, 3));
        teleplayAdapter = new TeleplayAdapter();
        mDataBinding.rvPraiseTeleplayList.setAdapter(teleplayAdapter);
        teleplayAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivScoreMovie://评分最高电影
                startActivity(ScoreMovieActivity.class);
                break;
            case R.id.ivTeleplayList://电视剧榜单
                startActivity(TeleplayListActivity.class);
                break;
            case R.id.ivTicketMovieMore://票房电影更多
                startActivity(TicketMovieActivity.class);
                break;
            case R.id.ivPraiseTeleplayMore://口碑电视剧更多
                startActivity(PraiseTeleplayActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == movieAdapter) {
            StkResBean bean = listMovie.get(position);
            MovieDetailActivity.stkResBean = bean;
            startActivity(MovieDetailActivity.class);
        } else {
            StkResBean bean = (StkResBean) adapter.getItem(position);
            MovieDetailActivity.stkResBean = bean;
            startActivity(MovieDetailActivity.class);
        }
    }

    @Override
    protected void initData() {
        List<Integer> list = new ArrayList<>();
        list.add(R.drawable.adiershitiao);
        list.add(R.drawable.arenshengdashi);
        list.add(R.drawable.acaomurenjian);
        list.add(R.drawable.ayuhuozhilu);
        movieAdapter.setList(list);
        getMovieData();
        getTeleplayData();
    }

    private void getMovieData() {
        StkResApi.getTagResourceList(this, AppConfig.HOME_MOVIE_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        listMovie.addAll(result);
                    }
                }
            }
        });
    }

    private void getTeleplayData() {
        StkResApi.getTagResourceList(this, AppConfig.HOME_TELEPLAY_DATA, StkResApi.createParamMap(0, 12), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        teleplayAdapter.setList(result);
                    }
                }
            }
        });
    }

}